{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e40fd8ca-67e5-46c0-b5ff-59df0e96b9ad",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-09-08T02:49:51.788165Z",
     "iopub.status.busy": "2025-09-08T02:49:51.787963Z",
     "iopub.status.idle": "2025-09-08T02:49:51.792873Z",
     "shell.execute_reply": "2025-09-08T02:49:51.792468Z",
     "shell.execute_reply.started": "2025-09-08T02:49:51.788150Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 使用英文\n",
    "docs_en = [\n",
    "    \"Machine learning is changing our way of life.\",\n",
    "    \"Deep learning performs exceptionally well in image recognition.\",\n",
    "    \"Natural language processing is an important field in computer science.\",\n",
    "    \"Autonomous driving relies on advanced algorithms.\",\n",
    "    \"AI can help doctors diagnose diseases.\",\n",
    "    \"Data analysis technology is widely applied in the financial field.\",\n",
    "    \"Production efficiency can be improved through automation technology.\",\n",
    "    \"The future of machine intelligence is full of potential.\",\n",
    "    \"Big data support is key to the development of machine intelligence.\",\n",
    "    \"The quantum tunneling effect allows electrons to pass through potential barriers that classical mechanics consider impassable, which has important applications in semiconductor devices.\"\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30ab8d6",
   "metadata": {},
   "source": [
    "使用modelscope的模型生成文档集合的稀疏向量，原文章中使用的是huggingface的\n",
    "```python\n",
    "model_name = \"naver/splade-cocondenser-selfdistil\"\n",
    "```\n",
    "\n",
    "国内无法访问，所以使用了\"ibm-granite/granite-embedding-30m-sparse\"模型，结果大差不差滴\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9d32dffa-1c4e-435a-a73d-5578dd6b758e",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-09-08T03:00:57.715220Z",
     "iopub.status.busy": "2025-09-08T03:00:57.714993Z",
     "iopub.status.idle": "2025-09-08T03:01:09.575516Z",
     "shell.execute_reply": "2025-09-08T03:01:09.575047Z",
     "shell.execute_reply.started": "2025-09-08T03:00:57.715205Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Model from https://www.modelscope.cn to directory: /mnt/workspace/.cache/modelscope/models/ibm-granite/granite-embedding-30m-sparse\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-09-08 11:00:59,167 - modelscope - INFO - Got 15 files, start to download ...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "da9b50d2a9414f888ddf21bb89da875a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Processing 15 items:   0%|          | 0.00/15.0 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a6e8cde87d8a479da8a8c04c1b526137",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [1_SpladePooling/config.json]:   0%|          | 0.00/106 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8bc6e8e023004fc9a0fb3cf0833af2d7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [config_sentence_transformers.json]:   0%|          | 0.00/274 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "829344e04412475792aea6d9cbd1ea89",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [config.json]:   0%|          | 0.00/688 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "223bc99b9d964f2aa54eb35821302f68",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [merges.txt]:   0%|          | 0.00/446k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d0e7484915d84ec58ea7e3cd0d4259a3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [model.safetensors]:   0%|          | 0.00/57.9M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a534612a3fc7416a8caab8b49ded065f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [configuration.json]:   0%|          | 0.00/77.0 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "854d15090b3143dfa8c1358dd01f43de",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [modules.json]:   0%|          | 0.00/274 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fb79ed3d799344c9b2a08de6dc4a2bc6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [pytorch_model.bin]:   0%|          | 0.00/296M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "46986bdb857e4013bbce530193900afa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [README.md]:   0%|          | 0.00/12.4k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0f0ca5450cf849e9a53098ff71eb29df",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [sentence_bert_config.json]:   0%|          | 0.00/57.0 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a6b0e98d85624fab8fb2ba7a06fd1caf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [special_tokens_map.json]:   0%|          | 0.00/239 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0020ae41dd5a4425a492de79c2dd463e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [tokenizer.json]:   0%|          | 0.00/1.29M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0781d0e870984b768dc2e2b0db6bd129",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [tokenizer_config.json]:   0%|          | 0.00/1.15k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "418a84a87bb3451cb996b014cd014757",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [vocab.json]:   0%|          | 0.00/780k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4a9a4f8c9c17406699f8e4aebf852813",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading [zero_to_fp32.py]:   0%|          | 0.00/23.1k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-09-08 11:01:03,034 - modelscope - INFO - Download model 'ibm-granite/granite-embedding-30m-sparse' successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型已下载至: /mnt/workspace/.cache/modelscope/models/ibm-granite/granite-embedding-30m-sparse\n",
      "  (0, 4)\t0.2877643406391144\n",
      "  (0, 52)\t0.4842591881752014\n",
      "  (0, 55)\t0.013229835778474808\n",
      "  (0, 92)\t0.2468601018190384\n",
      "  (0, 122)\t0.5920421481132507\n",
      "  (0, 169)\t0.03369762748479843\n",
      "  (0, 197)\t0.095177561044693\n",
      "  (0, 210)\t0.09415344893932343\n",
      "  (0, 301)\t1.1498292684555054\n",
      "  (0, 448)\t0.1370851695537567\n",
      "  (0, 464)\t1.1264784336090088\n",
      "  (0, 474)\t0.18824294209480286\n",
      "  (0, 505)\t0.03884601965546608\n",
      "  (0, 532)\t0.24697820842266083\n",
      "  (0, 555)\t0.026394851505756378\n",
      "  (0, 596)\t0.1440160721540451\n",
      "  (0, 622)\t0.37694254517555237\n",
      "  (0, 666)\t0.30827078223228455\n",
      "  (0, 712)\t0.1144619882106781\n",
      "  (0, 806)\t0.0590529665350914\n",
      "  (0, 1022)\t0.939770519733429\n",
      "  (0, 1050)\t0.22106267511844635\n",
      "  (0, 1131)\t0.3955904245376587\n",
      "  (0, 1437)\t0.5205164551734924\n",
      "  (0, 1477)\t0.45300784707069397\n",
      "  :\t:\n",
      "  (9, 35233)\t0.015529687516391277\n",
      "  (9, 35235)\t1.2405478954315186\n",
      "  (9, 35644)\t0.10154350847005844\n",
      "  (9, 35772)\t0.03416196629405022\n",
      "  (9, 36713)\t0.48418405652046204\n",
      "  (9, 37029)\t0.739937424659729\n",
      "  (9, 37113)\t0.8574795126914978\n",
      "  (9, 38130)\t0.44522544741630554\n",
      "  (9, 38622)\t0.20147016644477844\n",
      "  (9, 38970)\t0.7281038761138916\n",
      "  (9, 39276)\t0.26712676882743835\n",
      "  (9, 39816)\t0.29252883791923523\n",
      "  (9, 39832)\t0.45421722531318665\n",
      "  (9, 40584)\t0.24220602214336395\n",
      "  (9, 42521)\t0.23398907482624054\n",
      "  (9, 43275)\t0.042891725897789\n",
      "  (9, 43437)\t0.27172160148620605\n",
      "  (9, 43797)\t0.06886070966720581\n",
      "  (9, 44239)\t0.25739893317222595\n",
      "  (9, 44937)\t0.12527307868003845\n",
      "  (9, 44944)\t0.008730199187994003\n",
      "  (9, 44981)\t0.1655087172985077\n",
      "  (9, 44999)\t0.42940905690193176\n",
      "  (9, 45573)\t0.4418472647666931\n",
      "  (9, 47993)\t0.5035671591758728\n"
     ]
    }
   ],
   "source": [
    "from modelscope import snapshot_download\n",
    "from pymilvus.model.sparse import SpladeEmbeddingFunction\n",
    "\n",
    "query_en = [\"How does artificial intelligence affect the automotive industry?\"]\n",
    "\n",
    "model_name = \"ibm-granite/granite-embedding-30m-sparse\"\n",
    "\n",
    "# 下载模型到本地\n",
    "model_dir = snapshot_download(model_name)\n",
    "# 步骤2：使用本地模型路径初始化 SpladeEmbeddingFunction\n",
    "splade_ef = SpladeEmbeddingFunction(\n",
    "    model_name=model_dir,  \n",
    "    device=\"cpu\"          \n",
    ")\n",
    "\n",
    "# 确保模型加载成功\n",
    "print(f\"模型已下载至: {model_dir}\")\n",
    "\n",
    "\n",
    "# 生成文档集合的稀疏向量\n",
    "sparse_vectors_splade = splade_ef.encode_documents(docs_en)\n",
    "print(sparse_vectors_splade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "693d565c-1d6f-47ca-a1a6-d23c6f634246",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-09-08T03:04:29.855857Z",
     "iopub.status.busy": "2025-09-08T03:04:29.855635Z",
     "iopub.status.idle": "2025-09-08T03:04:29.943978Z",
     "shell.execute_reply": "2025-09-08T03:04:29.943525Z",
     "shell.execute_reply.started": "2025-09-08T03:04:29.855841Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "splade词汇表中的单词数量：50265\n",
      "splade稀疏向量维度：50265\n"
     ]
    }
   ],
   "source": [
    "# splade词汇表中的单词数量\n",
    "from transformers import AutoModelForMaskedLM, AutoTokenizer\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_dir)\n",
    "print(f\"splade词汇表中的单词数量：{tokenizer.vocab_size}\")\n",
    "\n",
    "print(f\"splade稀疏向量维度：{splade_ef.dim}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "74f5413f-fe4f-43aa-85f1-be8586bff0e6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-09-08T03:04:34.892702Z",
     "iopub.status.busy": "2025-09-08T03:04:34.892453Z",
     "iopub.status.idle": "2025-09-08T03:04:37.083621Z",
     "shell.execute_reply": "2025-09-08T03:04:37.083049Z",
     "shell.execute_reply.started": "2025-09-08T03:04:34.892687Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "“How does artificial intelligence affect the automotive industry?” 的分词结果：\n",
      "['How', 'Ġdoes', 'Ġartificial', 'Ġintelligence', 'Ġaffect', 'Ġthe', 'Ġautomotive', 'Ġindustry', '?']\n",
      "tokens数量：9\n",
      "  (0, 138)\t0.7028489708900452\n",
      "  (0, 210)\t0.15836457908153534\n",
      "  (0, 216)\t0.02560880221426487\n",
      "  (0, 250)\t0.0022231058683246374\n",
      "  (0, 505)\t0.2783266603946686\n",
      "  (0, 512)\t1.5052143335342407\n",
      "  (0, 539)\t1.3155847787857056\n",
      "  (0, 596)\t0.06378108263015747\n",
      "  (0, 806)\t0.09540452063083649\n",
      "  (0, 913)\t0.12891486287117004\n",
      "  (0, 921)\t0.23910488188266754\n",
      "  (0, 931)\t0.11032351851463318\n",
      "  (0, 1050)\t0.09377873688936234\n",
      "  (0, 1078)\t0.24715512990951538\n",
      "  (0, 1272)\t0.14475324749946594\n",
      "  (0, 1293)\t0.12612946331501007\n",
      "  (0, 1653)\t0.5624660849571228\n",
      "  (0, 1683)\t0.9857549667358398\n",
      "  (0, 2239)\t0.7662491798400879\n",
      "  (0, 2267)\t0.24620868265628815\n",
      "  (0, 2316)\t1.452373743057251\n",
      "  (0, 2655)\t0.14290887117385864\n",
      "  (0, 2900)\t0.18022646009922028\n",
      "  (0, 2903)\t0.04654909670352936\n",
      "  (0, 2991)\t0.14969828724861145\n",
      "  :\t:\n",
      "  (0, 6741)\t0.4997083246707916\n",
      "  (0, 7350)\t1.2752195596694946\n",
      "  (0, 8164)\t0.13824041187763214\n",
      "  (0, 8408)\t0.24117644131183624\n",
      "  (0, 8422)\t0.07205881178379059\n",
      "  (0, 8611)\t0.19876806437969208\n",
      "  (0, 9415)\t0.5774601697921753\n",
      "  (0, 9719)\t0.7817707657814026\n",
      "  (0, 9916)\t0.23191127181053162\n",
      "  (0, 10561)\t0.26905032992362976\n",
      "  (0, 11767)\t0.12424663454294205\n",
      "  (0, 12340)\t0.5059556365013123\n",
      "  (0, 13044)\t0.1946694701910019\n",
      "  (0, 13557)\t0.06795016676187515\n",
      "  (0, 15064)\t0.0776582732796669\n",
      "  (0, 15221)\t0.22061820328235626\n",
      "  (0, 16782)\t0.1881699115037918\n",
      "  (0, 17909)\t0.4631165564060211\n",
      "  (0, 19896)\t0.1822943240404129\n",
      "  (0, 20257)\t0.07473737001419067\n",
      "  (0, 23931)\t0.3438626825809479\n",
      "  (0, 28310)\t0.6411561369895935\n",
      "  (0, 31024)\t0.009720668196678162\n",
      "  (0, 39974)\t0.28803518414497375\n",
      "  (0, 42752)\t0.06604843586683273\n"
     ]
    }
   ],
   "source": [
    "# 查看查询的分词\n",
    "tokens = tokenizer.tokenize(query_en[0])\n",
    "print(f\"“{query_en[0]}” 的分词结果：\\n{tokens}\")\n",
    "print(f\"tokens数量：{len(tokens)}\")\n",
    "\n",
    "# 生成查询的稀疏向量\n",
    "query_sparse_vectors_splade = splade_ef.encode_queries(query_en)\n",
    "print(query_sparse_vectors_splade)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0546ee6",
   "metadata": {},
   "source": [
    "观察结果可以发现分词数量和稀疏向量的维度不一样。这就是splade和BM25的重要区别，splade可以举一反三，在最初9个分词的基础上，又增加了其他语义相近的单词。\n",
    "\n",
    "那么，查询现在一共有多少个单词呢？或者说，它的稀疏向量的非零元素有多少呢？\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b49d5882-4ac2-4252-85c2-f1a64248dd82",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-09-08T03:04:39.242758Z",
     "iopub.status.busy": "2025-09-08T03:04:39.242535Z",
     "iopub.status.idle": "2025-09-08T03:04:39.248422Z",
     "shell.execute_reply": "2025-09-08T03:04:39.248005Z",
     "shell.execute_reply.started": "2025-09-08T03:04:39.242742Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "splade 稀疏向量非零元素数量：67\n"
     ]
    }
   ],
   "source": [
    "# 获取稀疏向量的非零索引\n",
    "nonzero_indices = query_sparse_vectors_splade.indices[query_sparse_vectors_splade.indptr[0]:query_sparse_vectors_splade.indptr[1]]\n",
    "\n",
    "# 构建稀疏词权重列表\n",
    "sparse_token_weights = [\n",
    "    (splade_ef.model.tokenizer.decode(col), query_sparse_vectors_splade[0, col])\n",
    "    for col in nonzero_indices\n",
    "]\n",
    "\n",
    "# 按权重降序排序\n",
    "sparse_token_weights = sorted(sparse_token_weights, key=lambda item: item[1], reverse=True)\n",
    "\n",
    "# 查询句只有9个tokens，splade通过举一反三，生成的稀疏向量维度增加到了98个。\n",
    "print(f\"splade 稀疏向量非零元素数量：{len(sparse_token_weights)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "77d6bbaf-e707-4158-9899-360fe940ed70",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-09-08T03:04:41.318990Z",
     "iopub.status.busy": "2025-09-08T03:04:41.318760Z",
     "iopub.status.idle": "2025-09-08T03:04:41.322219Z",
     "shell.execute_reply": "2025-09-08T03:04:41.321795Z",
     "shell.execute_reply.started": "2025-09-08T03:04:41.318953Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(' AI', 1.5240402)\n",
      "(' car', 1.5052143)\n",
      "(' intelligence', 1.4523737)\n",
      "(' industry', 1.3155848)\n",
      "(' affect', 1.3052505)\n",
      "(' artificial', 1.2752196)\n",
      "(' Motor', 1.0611075)\n",
      "(' effect', 0.98575497)\n",
      "(' auto', 0.94583946)\n",
      "('otive', 0.78177077)\n",
      "(' learning', 0.7662492)\n",
      "(' company', 0.702849)\n",
      "('econom', 0.64115614)\n",
      "(' computer', 0.60182554)\n",
      "(' bias', 0.57746017)\n",
      "(' Car', 0.5624661)\n",
      "(' advantages', 0.50595564)\n",
      "(' Industrial', 0.49970832)\n",
      "(' Analytics', 0.46311656)\n",
      "(' engineering', 0.46053436)\n",
      "(' manufacturing', 0.45494542)\n",
      "(' advancements', 0.34386268)\n",
      "(' machine', 0.33768395)\n",
      "(' regression', 0.28803518)\n",
      "(' important', 0.27832666)\n",
      "(' philosophy', 0.26905033)\n",
      "(' Data', 0.25442016)\n",
      "(' safety', 0.24715513)\n",
      "(' consumer', 0.24620868)\n",
      "(' automatic', 0.24117644)\n",
      "(' road', 0.23910488)\n",
      "(' engine', 0.2355993)\n",
      "(' robot', 0.23191127)\n",
      "(' Autom', 0.23061593)\n",
      "(' PhD', 0.2206182)\n",
      "(' accuracy', 0.19876806)\n",
      "(' factor', 0.19704793)\n",
      "(' IoT', 0.19466947)\n",
      "(' prediction', 0.18816991)\n",
      "('EV', 0.18229432)\n",
      "(' brain', 0.18022646)\n",
      "(' skill', 0.16307092)\n",
      "(' market', 0.15836458)\n",
      "(' decline', 0.14969829)\n",
      "(' problems', 0.14475325)\n",
      "(' knowledge', 0.14290887)\n",
      "(' Information', 0.14202787)\n",
      "(' frustrated', 0.13824041)\n",
      "(' impact', 0.12891486)\n",
      "(' innovation', 0.12877584)\n",
      "(' sector', 0.12612946)\n",
      "(' automation', 0.124246635)\n",
      "(' production', 0.11032352)\n",
      "(' technology', 0.09540452)\n",
      "(' human', 0.09377874)\n",
      "(' aerospace', 0.07765827)\n",
      "(' classification', 0.07473737)\n",
      "(' toxic', 0.07205881)\n",
      "(' ethical', 0.06795017)\n",
      "(' inference', 0.066048436)\n",
      "(' why', 0.06378108)\n",
      "(' tech', 0.046549097)\n",
      "(' consequences', 0.045247525)\n",
      "(' regulation', 0.026370712)\n",
      "(' know', 0.025608802)\n",
      "(' Robot', 0.009720668)\n",
      "('A', 0.0022231059)\n"
     ]
    }
   ],
   "source": [
    "# 比如，和“artificial intelligence”语义相近的 “ai”，和“automotive”语义相近的“car”。\n",
    "for token in sparse_token_weights:\n",
    "    print(token)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e04aa8f4",
   "metadata": {},
   "source": [
    "splade增加了大量语义相近的单词，比如和“artificial intelligence”语义相近的 “ai”，和“automotive”语义相近的“car”。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
